function clearAll(form) {
	for (var i=0; i<form.elements.length; i++) {
		var elementType = form.elements[i].type;
		//alert(elementType);
		if (elementType == "button" || elementType == "reset" || elementType == "submit" || elementType == "hidden") {
			;//do nothing
		} else if (elementType == "select-one") {
			form.elements[i].value="-1";
		} else if (elementType == "radio") {
			form.elements[i].checked="false";
			form.elements[i].value="-1";
		} else if (elementType == "text") {
			form.elements[i].value="";
		} else {
			form.elements[i].value="";
		}
	}
}

function isNumerical(value) {
	var bValid = true;
	var regform = / /g;
	value = value.replace(regform, "");

	if (value.length > 0) {
		if (isNaN(value)) {
			bValid = false;
	   }
		if (bValid) {
			var numbers = "0123456789-.";
			for (var i=0; i<value.length; i++) {
				if (numbers.indexOf(value.charAt(i)) == -1) {
					bValid = false;
					break;
				}
			}
		}
	}
	return bValid;
}

function isInteger(value) {
	var bValid = true;
	var regform = / /g;
	value = value.replace(regform, "");

	if (value.length > 0) {
		if (isNaN(value)) {
			bValid = false;
	   }
		if (bValid) {
			var numbers = "0123456789-";
			for (var i=0; i<value.length; i++) {
				if (numbers.indexOf(value.charAt(i)) == -1) {
					bValid = false;
					break;
				}
			}
		}
	}
	return bValid;
}


function del(url) {
	if (confirm("您確定要刪除嗎?")) {
		window.location.href = url;
	}	
}

function sbar(st) {st.style.backgroundColor='#D6BED6';}
function cbar(st) {st.style.backgroundColor='';}

function isEmpty(value) {
	var regform = / /g;
	var flag = false;
	if (value.replace(regform, "").length == 0) {
		flag = true;
	}
	return flag;
}

function isChineseExist(value) {
	var bValid = false;
	value = escape(value);
	if (value.indexOf("%u") != -1) {
		bValid = true;
	}
	return bValid;
}

function isAllNumber(value) {
	var bValid = true;
	if (!isEmpty(value)) {
		var keywords = "0123456789";
		for (var i=0; i<value.length; i++) {
			if (keywords.indexOf(value.charAt(i)) == -1) {
				bValid = false;
				break;
			}
		}
	} else {
		bValid = false;
	}
	return bValid;
}

function isAllEnglish(value) {
	var bValid = true;
	if (!isEmpty(value)) {
		value = value.toLowerCase();
		var keywords = "abcdefghijklmnopqrstuvwxyz";
		for (var i=0; i<value.length; i++) {
			if (keywords.indexOf(value.charAt(i)) == -1) {
				bValid = false;
				break;
			}
		}
	} else {
		bValid = false;
	}
	return bValid;
}

function addSelectedIndex (srcObj, destObj) {	
	var src = eval(srcObj);
	var dest = eval(destObj);	
	if (src.options == null || dest.options == null) {
		return false;
	}	
	if (src.selectedIndex==-1 || src.selectedIndex == 0)
		return false;
	if (src.length > 0) {	
		if (dest.length <= 0) dest.length = 0;
		dest.length += 1;	
		dest.options[dest.length - 1].text = src.options[src.selectedIndex].text;
		dest.options[dest.length - 1].value = src.options[src.selectedIndex].value;	
		for (var i = src.selectedIndex; i< (src.length-1); i++) {		
			src.options[i].text = src.options[i+1].text;
			src.options[i].value = src.options[i+1].value;
		}	
		src.length = src.length -1;
		//src.selectedIndex = 0;
	}	
}

function removeSelectedIndex (srcObj, destObj) {
	var src = eval(srcObj);
	var dest = eval(destObj);	
	if (src.options == null || dest.options == null) {
		return false;
	}	
	if (dest.selectedIndex==-1 || dest.selectedIndex == 0)
		return false;
	if (dest.length > 0) {		
		if (src.length <= 0) src.length = 0;
		src.length += 1;	
		src.options[src.length - 1].text = dest.options[dest.selectedIndex].text;
		src.options[src.length - 1].value = dest.options[dest.selectedIndex].value;		
		for (var i = dest.selectedIndex; i< (dest.length-1); i++) {		
			dest.options[i].text = dest.options[i+1].text;
			dest.options[i].value = dest.options[i+1].value;
		}		
		dest.length = dest.length - 1;
		//dest.selectedIndex = 0;	
	}	
}

function validateEmail(email) {
	var regform = / /g;
	var errormsg = "";
	var bValid = true;
	var value = email;
	value = value.replace(regform, "");
	if (isEmpty(value))
		return false;
	return checkEmail(value);	
}

function checkEmail(emailStr) {
   var regform = / /g;
   if (emailStr.length == 0) {
	   return false;
   }
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
   var matchArray=emailStr.match(emailPat);
   if (matchArray == null) {
	   return false;
   }
   var user=matchArray[1];
   var domain=matchArray[2];
   if (user.match(userPat) == null) {
	   return false;
   }
   var IPArray = domain.match(ipDomainPat);
   if (IPArray != null) {
	   for (var i = 1; i <= 4; i++) {
		  if (IPArray[i] > 255) {
			 return false;
		  }
	   }
	   return true;
   }
   var domainArray=domain.match(domainPat);
   if (domainArray == null) {
	   return false;
   }
   var atomPat=new RegExp(atom,"g");
   var domArr=domain.match(atomPat);
   var len=domArr.length;
   if ((domArr[domArr.length-1].length < 2) ||
	   (domArr[domArr.length-1].length > 3)) {
	   return false;
   }
   if (len < 2) {
	   return false;
   }
   return true;
}

function isDate(year, month, day) {
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);

	if ( (tempDate.getYear()==year) &&	(month == tempDate.getMonth()) &&	(day == tempDate.getDate()) ) 
		return true;
	else
		return false;
}

// default ROC-format: YYY/MM/DD
function isROCDate(value) {
	var len = value.length;

	if (len != 9)
		return false;

	var index1 = value.substring(3, 4);
	var index2 = value.substring(6, 7);

	if (index1 != "/" || index2 != "/") {
		return false;
	}

	var rocYear = value.substring(0, 3);
	var rocMonth = value.substring(4, 6);
	var rocDate = value.substring(7);

	if (!isNumerical(rocYear) || 
		!isNumerical(rocMonth) ||
		!isNumerical(rocDate)) {
		return false;
	}

	var regform = / /g;
	var rocYearLen = rocYear.replace(regform, "").length;
	if (rocYearLen != 3) {
		return false;
	}

	var westYear = parseInt(rocYear, 10) + 1911;
	var westMonth = parseInt(rocMonth, 10);
	var westDate = parseInt(rocDate, 10);

	return isDate(westYear, westMonth, westDate);

}

function convertStringToUniCode(source) {
  sret = "";
  // use Unicode escape to  \u00ff
  for(var i=0; i < source.length; i++) {
     var num = new Number( source.charCodeAt(i));
     sret += "\\u" + num.toString(16);
  }
  return sret;
}

function checkDate(year,month,day) {

	var datemsg = "";
	var datestatus = true;
	
	if (month < 1 || month > 12) { // check month range
		datemsg=datemsg + "----- Month must be between 1 and 12." + "\n";
		alert(datemsg);
		datestatus=false;
	}
	
	if (day < 1 || day > 31) {
		datemsg=datemsg + "----- Day must be between 1 and 31." + "\n";
		alert(datemsg);
		datestatus=false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		datemsg=datemsg + "----- Month " + month + " doesn`t have 31 days!" + "\n";
		datestatus=false;
	}
	
	if (month == 2) { 
		// check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
			datemsg=datemsg + "----- February " + year + " doesn`t have " + day + " days!" + "\n";
			datestatus=false;
		}

	}
	//	alert(datemsg);
	return datestatus;
}

/**
 * 轉大寫 
 */
function InputtoUpperCase(event) {  
	if (event.keyCode>=97 && event.keyCode<=122)
		event.keyCode -= 32;
} 


/**
 * 限制使用者只能輸入數字0-9
 */
function InputOnlyNum(event) {  
	if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) 
		event.returnValue=false;
} 

/**
 * 限制使用者只能輸入正浮點數字
 */
function InputOnlyPositiveFloat(event) {  
	if (event.keyCode!=46 && (event.keyCode<48 || event.keyCode>57)) 
		event.returnValue=false;
} 

/**
 * 限制使用者只能輸入正整數
 */
function InputOnlyPositiveInt(event) {  
	if ( (event.keyCode<48 || event.keyCode>57)) 
		event.returnValue=false;
} 

/**
 * 限制使用者只能輸入0-9,A-Z,a-z
 */
function InputOnlyEngAndNum(event) {  
	if ( !((event.keyCode>=48 && event.keyCode<=57)  || (event.keyCode>=65 && event.keyCode<=90) ||  (event.keyCode>=97 && event.keyCode<=122) ) ) 
		event.returnValue=false;
} 

/**
 * 限制使用者只能輸入0-9,A-Z,a-z
 * 並自動轉換大小寫
 */
function InputOnlyIdno(event) {  
	if ( !((event.keyCode>=48 && event.keyCode<=57)  || (event.keyCode>=65 && event.keyCode<=90) ||  (event.keyCode>=97 && event.keyCode<=122) ) ) 
		event.returnValue=false;
	
	if (event.keyCode>=97 && event.keyCode<=122)
		event.keyCode -= 32;
} 

	/**
 * 限制使用者只能輸入0-9,A-Z,a-z,*
 * 並自動轉換大小寫
 */
function InputOnlyIdno2(event) {  
	if ( !((event.keyCode>=48 && event.keyCode<=57)  || (event.keyCode>=65 && event.keyCode<=90) ||  (event.keyCode>=97 && event.keyCode<=122) ||  (event.keyCode==42)) ) 
		event.returnValue=false;
	
	if (event.keyCode>=97 && event.keyCode<=122)
		event.keyCode -= 32;
}

/**
 * 檢查是否為正整數
 */

function isNotPositiveInteger(theString){
	var theData = new String(theString);
	var re5digit = /^\d+$/;

	for(var i=1; i<theData.length; i++){
		if(theData.search(re5digit)==-1)
			return true;
	}
}

function checkSubmit(form) {
	return true;
	if (form.actionType == null || form.actionType.value == null || trim(form.actionType.value).length == 0) {
		return false;
	}
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
	s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
	s = s.substring(0,s.length-1);
  }
  return s;
}

/**
 * 檢查身分證字號或統編是否正常
 */
function checkID(id){
	letter ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	i=0;
	result=0;

	id=id.toUpperCase(); // 轉成大寫
	id=id.replace(" /g", ""); // 把空白換掉
	if(id.length != 10) // 是不是10個字?
	{
		if ( id.length == 8 )	return chknumCompanyNo(id);
		alert("身分證長度不正確。");
		return false;
	}
	if(letter.indexOf(id.substring(0,1)) == -1)
	{
		alert("身分證第一個字母必須是英文:o。");
		return false;
	} 
	else {
		i = letter.indexOf(id.substring(0,1)) + 10;
	}
	
	result=0;

	if(isNaN(id.substring(1,10)))
	{
		alert("身分證後面九碼必須是數字。");
		return false;
	}

	if(id.substring(1,2) != "1" && id.substring(1,2) != "2")
	{
		alert("身分證性別錯誤。");
		return false;
	}

	idalpha = id.substring(0,1);
	idalpha_value= 0;

	if( idalpha == "A" )  idalpha_value = 1;
	else if( idalpha == "B" )  idalpha_value = 10;
	else if( idalpha == "C" )  idalpha_value = 19;
	else if( idalpha == "D" )  idalpha_value = 28;
	else if( idalpha == "E" )  idalpha_value = 37;
	else if( idalpha == "F" )  idalpha_value = 46;
	else if( idalpha == "G" )  idalpha_value = 55;
	else if( idalpha == "H" )  idalpha_value = 64;
	else if( idalpha == "I" )  idalpha_value = 39;
	else if( idalpha == "J" )  idalpha_value = 73;
	else if( idalpha == "K" )  idalpha_value = 82;
	else if( idalpha == "L" )  idalpha_value = 2;
	else if( idalpha == "M" )  idalpha_value = 11;
	else if( idalpha == "N" )  idalpha_value = 20;
	else if( idalpha == "O" )  idalpha_value = 48;
	else if( idalpha == "P" )  idalpha_value = 29;
	else if( idalpha == "Q" )  idalpha_value = 38;
	else if( idalpha == "R" )  idalpha_value = 47;
	else if( idalpha == "S" )  idalpha_value = 56;
	else if( idalpha == "T" )  idalpha_value = 65;
	else if( idalpha == "U" )  idalpha_value = 74;
	else if( idalpha == "V" )  idalpha_value = 83;
	else if( idalpha == "W" )  idalpha_value = 21;
	else if( idalpha == "X" )  idalpha_value = 3;
	else if( idalpha == "Y" )  idalpha_value = 12;
	else if( idalpha == "Z" )  idalpha_value = 30;


	for(i=1; i<9; i++)
	{
		result += (parseInt(id.substring(i, i+1)) * (9-i));
	}

	result = result+idalpha_value;

	if( ((10 - (result % 10))%10) != id.substring(9,10))
	{
		alert("身分證錯誤！");
		return false;
	}

	return true;
}



/**
 * 檢查身分證字號是否正常
 */
function checkOnlyID(id){
	letter ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	i=0;
	result=0;

	id=id.toUpperCase(); // 轉成大寫
	id=id.replace(" /g", ""); // 把空白換掉
	if(id.length != 10) // 是不是10個字?
	{
		//alert("身分證長度不正確。");
		return false;
	}
	if(letter.indexOf(id.substring(0,1)) == -1)
	{
		//alert("身分證第一個字母必須是英文:o。");
		return false;
	} 
	else {
		i = letter.indexOf(id.substring(0,1)) + 10;
	}
	
	result=0;

	if(isNaN(id.substring(1,10)))
	{
		//alert("身分證後面九碼必須是數字。");
		return false;
	}

	if(id.substring(1,2) != "1" && id.substring(1,2) != "2")
	{
		//alert("身分證性別錯誤。");
		return false;
	}

	idalpha = id.substring(0,1);
	idalpha_value= 0;

	if( idalpha == "A" )  idalpha_value = 1;
	else if( idalpha == "B" )  idalpha_value = 10;
	else if( idalpha == "C" )  idalpha_value = 19;
	else if( idalpha == "D" )  idalpha_value = 28;
	else if( idalpha == "E" )  idalpha_value = 37;
	else if( idalpha == "F" )  idalpha_value = 46;
	else if( idalpha == "G" )  idalpha_value = 55;
	else if( idalpha == "H" )  idalpha_value = 64;
	else if( idalpha == "I" )  idalpha_value = 39;
	else if( idalpha == "J" )  idalpha_value = 73;
	else if( idalpha == "K" )  idalpha_value = 82;
	else if( idalpha == "L" )  idalpha_value = 2;
	else if( idalpha == "M" )  idalpha_value = 11;
	else if( idalpha == "N" )  idalpha_value = 20;
	else if( idalpha == "O" )  idalpha_value = 48;
	else if( idalpha == "P" )  idalpha_value = 29;
	else if( idalpha == "Q" )  idalpha_value = 38;
	else if( idalpha == "R" )  idalpha_value = 47;
	else if( idalpha == "S" )  idalpha_value = 56;
	else if( idalpha == "T" )  idalpha_value = 65;
	else if( idalpha == "U" )  idalpha_value = 74;
	else if( idalpha == "V" )  idalpha_value = 83;
	else if( idalpha == "W" )  idalpha_value = 21;
	else if( idalpha == "X" )  idalpha_value = 3;
	else if( idalpha == "Y" )  idalpha_value = 12;
	else if( idalpha == "Z" )  idalpha_value = 30;


	for(i=1; i<9; i++)
	{
		result += (parseInt(id.substring(i, i+1)) * (9-i));
	}

	result = result+idalpha_value;

	if( ((10 - (result % 10))%10) != id.substring(9,10))
	{
		//alert("身分證錯誤！");
		return false;
	}

	return true;
}


	var cx = new Array;
	cx[0] = 1;
	cx[1] = 2;
	cx[2] = 1;
	cx[3] = 2;
	cx[4] = 1;
	cx[5] = 2;
	cx[6] = 4;
	cx[7] = 1;

/**
 * 檢查是否為正常之統編
 */
function chknumCompanyNo(NO){ 
	var SUM = 0;
	if (NO.length != 8) {
		alert("統編錯誤，要有 8 個數字");
		return;
	}
	var cnum = NO.split("");
	for (i=0; i<=7; i++) {
		if (NO.charCodeAt() < 48 || NO.charCodeAt() > 57) 
		{
			alert("統編錯誤，要有 8 個 0-9 數字組合");
			return;
		}
		SUM += cc(cnum[i] * cx[i]);
	}

	if (SUM % 10 == 0) return true;
	else if (cnum[6] == 7 && (SUM + 1) % 10 == 0) return true;
	else {
		alert("統 編 ： "+NO+" 錯 誤 !");
		return false;
	}
}

/**
 * 檢查是否為正常之統編
 */
function chknumCompanyNo2(NO){ 
	var SUM = 0;
	if (NO.length == 1) {
		if(NO == '*'){
			return true;
		}
	}

	if (NO.length != 8) {
		alert("統編錯誤，要有 8 個數字");
		return;
	}
	var cnum = NO.split("");
	for (i=0; i<=7; i++) {
		if (NO.charCodeAt() < 48 || NO.charCodeAt() > 57) 
		{
			alert("統編錯誤，要有 8 個 0-9 數字組合");
			return;
		}
		SUM += cc(cnum[i] * cx[i]);
	}

	if (SUM % 10 == 0) return true;
	else if (cnum[6] == 7 && (SUM + 1) % 10 == 0) return true;
	else {
		alert("統 編 ： "+NO+" 錯 誤 !");
		return false;
	}
}

function cc(n){
	if (n > 9) {
		var s = n + "";
		n1 = s.substring(0,1) * 1;
		n2 = s.substring(1,2) * 1;
		n = n1 + n2;
	}
	return n;
}


function isEmailValid(checkThisEmail)
{
	var myEMailIsValid = true;
	var myAtSymbolAt = checkThisEmail.indexOf('@');
	var myLastDotAt = checkThisEmail.lastIndexOf('.');
	var mySpaceAt = checkThisEmail.indexOf(' ');
	var myLength = checkThisEmail.length;


	// at least one @ must be present and not before position 2
	// @yellow.com : NOT valid
	// x@yellow.com : VALID

	if (myAtSymbolAt < 1 ) 
	{myEMailIsValid = false;}


	// at least one . (dot) afer the @ is required
	// x@yellow : NOT valid
	// x.y@yellow : NOT valid
	// x@yellow.org : VALID

	if (myLastDotAt < myAtSymbolAt) 
	{myEMailIsValid = false;}

	// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
	// x.y@yellow. : NOT valid
	// x.y@yellow.a : NOT valid
	// x.y@yellow.ca : VALID

	if (myLength - myLastDotAt <= 2) 
	{myEMailIsValid = false;}


	// no empty space " " is permitted (one may trim the email)
	// x.y@yell ow.com : NOT valid

	if (mySpaceAt != -1) 
	{myEMailIsValid = false;}

	return myEMailIsValid;
}

function isValidDate(year, month, day) {
		
		if(isNaN(day) || isNaN(month) || isNaN(year)){
			return false;
		}

		var regform = / /g;
		if (month < 1 || month > 12) {
				return false;
		}
		if (day < 1 || day > 31) {
			return false;
		}
		if ((month == 4 || month == 6 || month == 9 || month == 11) &&
			(day == 31)) {
			return false;
		}
		if (month == 2) {
			var leap = (year % 4 == 0 &&
					   (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day == 29 && !leap)) {
				return false;
			}
		}
		return true;
} 

function checkDate(obj){
		var str = obj.value;

		if(str == ""){
			return true;
		}
		if(str.length != 10){
			//alert( name + "格式錯誤" );
			//obj.select();
			return false;
		}
		
		if( str.indexOf("/") != 4){
			//alert( name + "格式錯誤" );
			//obj.select();
			return false;
		}
		if( str.lastIndexOf("/") != 7){
			//alert( name + "格式錯誤" );
			//obj.select();
			return false;
		}

		var dateStrAry = str.split("/");

		if(dateStrAry.length == 3){		
			if(! isValidDate(dateStrAry[0], 
						dateStrAry[1], 
						dateStrAry[2]) )
			{
				//alert( name + "格式錯誤" );
				//obj.select();
				return false;
			}
		}else{
			return false;
		}
		return true;
}

function onlyNumbers(evt) {
	var e = evt;
	if(window.event){ // IE
		var charCode = e.keyCode;
	} else if (e.which) { // Safari 4, Firefox 3.0.4
		var charCode = e.which;
	}
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	
	return true;
}

//半形轉全形
function unAsc(text) {  
	var asciiTable = "!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
	var big5Table = "%uFF01%u201D%uFF03%uFF04%uFF05%uFF06%u2019%uFF08%uFF09%uFF0A%uFF0B%uFF0C%uFF0D%uFF0E%uFF0F%uFF10%uFF11%uFF12%uFF13%uFF14%uFF15%uFF16%uFF17%uFF18%uFF19%uFF1A%uFF1B%uFF1C%uFF1D%uFF1E%uFF1F%uFF20%uFF21%uFF22%uFF23%uFF24%uFF25%uFF26%uFF27%uFF28%uFF29%uFF2A%uFF2B%uFF2C%uFF2D%uFF2E%uFF2F%uFF30%uFF31%uFF32%uFF33%uFF34%uFF35%uFF36%uFF37%uFF38%uFF39%uFF3A%uFF3B%uFF3C%uFF3D%uFF3E%uFF3F%u2018%uFF41%uFF42%uFF43%uFF44%uFF45%uFF46%uFF47%uFF48%uFF49%uFF4A%uFF4B%uFF4C%uFF4D%uFF4E%uFF4F%uFF50%uFF51%uFF52%uFF53%uFF54%uFF55%uFF56%uFF57%uFF58%uFF59%uFF5A%uFF5B%uFF5C%uFF5D%uFF5E";
	var result = "";
	for ( var i = 0 ; i < text.length ; i ++ ) {
	    var val = text.charAt(i) ;            
	    var j = asciiTable.indexOf(val) * 6 ;        
	    result += ( j > -1 ? unescape(big5Table.substring( j , j + 6 ) ) : val );    
	}
	return result;
}

function onlyFullForm(textFieldObj) {
	// 將ascii(半型)轉換成big5(全型)
	textFieldObj.value = unAsc(textFieldObj.value);
} // end function passSelf

/**
 * 限制使用者只能輸入日期0-9,-,/
 */
function InputOnlyDateFormat(event) {  
	if (event.keyCode!=45 && event.keyCode!=47 && (event.keyCode<48 || event.keyCode>57)) 
		event.returnValue=false;
} 

//檢查身分識別號    ----> 包括身分證,居留證,護照號
//用於驗證控制項
function check_id(textField,idKind,isValid){
	
	var id = textField.value.toUpperCase();
	var kind;
	
	// 'Y': 身分證
	if (idKind == 'Y') {
		kind = idKind;
	} // end if
	// 'N': 居留證
	else if (idKind == 'N') {
		kind = idKind;
	} // end if
	// 'P': 護照號
	else if (idKind == 'P') {
		kind = idKind;
	} // end if
	
	
	//檢查身分證字號
	if (kind=='Y'){
		if (id.length != 10) return(isValid=false);

		if (isNaN(id.substr(1,9)) || (id.substr(0,1)<"A" ||id.substr(0,1)>"Z")){
			return(isValid=false);					
		}

		var head="ABCDEFGHJKLMNPQRSTUVXYWZIO";
		id = (head.indexOf(id.substring(0,1))+10) +''+ id.substr(1,9);
		s =parseInt(id.substr(0,1)) + 
		parseInt(id.substr(1,1)) * 9 + 
		parseInt(id.substr(2,1)) * 8 + 
		parseInt(id.substr(3,1)) * 7 + 			
		parseInt(id.substr(4,1)) * 6 + 
		parseInt(id.substr(5,1)) * 5 + 
		parseInt(id.substr(6,1)) * 4 + 
		parseInt(id.substr(7,1)) * 3 + 
		parseInt(id.substr(8,1)) * 2 + 
		parseInt(id.substr(9,1)) + 
		parseInt(id.substr(10,1));

		//判斷是否可整除
		if ((s % 10) != 0) return(isValid=false);

		//身分證字號正確		
		return(isValid=true);
	    
	//檢查居留證號碼
	}else if  (kind=='N'){
		if (id.length != 10) return(isValid=false);

		if (isNaN(id.substr(2,8)) || (id.substr(0,1)<"A" ||id.substr(0,1)>"Z") || (id.substr(1,1)<"A" ||id.substr(1,1)>"Z")){
			return(isValid=false);					
		}
		
		var head="ABCDEFGHJKLMNPQRSTUVXYWZIO";
		id = (head.indexOf(id.substr(0,1))+10) +''+ ((head.indexOf(id.substr(1,1))+10)%10) +''+ id.substr(2,8);
		s =parseInt(id.substr(0,1)) + 
		parseInt(id.substr(1,1)) * 9 + 
		parseInt(id.substr(2,1)) * 8 + 
		parseInt(id.substr(3,1)) * 7 + 			
		parseInt(id.substr(4,1)) * 6 + 
		parseInt(id.substr(5,1)) * 5 + 
		parseInt(id.substr(6,1)) * 4 + 
		parseInt(id.substr(7,1)) * 3 + 
		parseInt(id.substr(8,1)) * 2 + 
		parseInt(id.substr(9,1)) + 
		parseInt(id.substr(10,1));

		//判斷是否可整除
		if ((s % 10) != 0) return(isValid=false);
		//居留證號碼正確		
		return(isValid=true);
	//檢查護照號碼
	}else if  (kind=='P'){
		if (id.length > 20) return(isValid=false);
		//護照號碼正確		
		return(isValid=true);
	//其他錯誤的格式	
	}else{
		return(isValid=false);
	}
}

// 判斷是否為潤年leapyear
function leapyear(a) {
	if (((a%4 == 0) && (a%100 != 0)) || (a%400 == 0))
		return true;
	else
		return false;
} // end function leapyear

function buildTextfieldToDate(textfieldObj) {
	
	if ( !checkDate(textfieldObj)) {
		alert("日期格式不正確");
		return null;
	} // end if
	else {
		var dateStr = textfieldObj.value;
	    var YYYY = new Number(dateStr.substr(0,4));
	    var MM = new Number(dateStr.substr(5,7).substr(0,2));
	    var DD = new Number(dateStr.substr(8,10));
	    
	    var date = new Date();
	    date.setFullYear(YYYY);
	    date.setMonth(MM-1);
	    date.setDate(DD);
	    
	    return date;
	} // end else
	
} // end function buildToDate

function initCalculateAge(startDateTextfield, endDateTextfield){
	
	var strStr;
	
	// 日期格式不正確
	if ( !checkDate(startDateTextfield) || !checkDate(endDateTextfield)) {
		//alert("日期格式不正確");
		return null;
	} // end if
	else if (buildTextfieldToDate(startDateTextfield)>buildTextfieldToDate(endDateTextfield)) {
		//alert("起始日期晚於終止日期");
		return null;
	} // end if
	else {
		strStr = startDateTextfield.value;
		var dateStrAry = strStr.split("/");	// YYYY/MM/DD
		return calculateAge(dateStrAry[0],dateStrAry[1],dateStrAry[2],buildTextfieldToDate(endDateTextfield));
	} // end else
		
} // end function calculateAge

function calculateAge( yyyy, mm, dd, days ) {

	var age;
	
	// days			生效日(Date物件)(日期較晚)
	// yyyy, mm, dd	出生年月日(日期較早)
	// 年齡=生效日-出生年月日
	var gdate = days.getDate();
	var gmonth = days.getMonth();
	var gyear = days.getFullYear();
	
	age = gyear - yyyy;
	if ((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
		age = age; 
	} // end if
	else if (mm < (gmonth + 1)) {
		age = age;
	} // end if
	else {
		age = age - 1; 
	} // end else
	
	if (age == 0)
		age = age;

	var m;
	var n;
	
	var weeks;		// 你出生至今 已經 " + weeks + " 個星期了
	var months;		// 你出生至今 已經 " + months + " 個月了
	var totdays;	// 你出生至今 已經 " + totdays + " 天了
	
	if (mm == 12) n = 31 - dd;
	if (mm == 11) n = 61 - dd;
	if (mm == 10) n = 92 - dd;
	if (mm == 9) n = 122 - dd;
	if (mm == 8) n = 153 - dd;
	if (mm == 7) n = 184 - dd;
	if (mm == 6) n = 214 - dd;
	if (mm == 5) n = 245 - dd;
	if (mm == 4) n = 275 - dd;
	if (mm == 3) n = 306 - dd;
	if (mm == 2) { n = 334 - dd; if (leapyear(yyyy)) n++; }
	if (mm == 1) { n = 365 - dd; if (leapyear(yyyy)) n++; }
	if (gmonth == 1) m = 31;
	if (gmonth == 2) { m = 59;if (leapyear(gyear)) m++;}
	if (gmonth == 3)  { m = 90;  if (leapyear(gyear)) m++; }
	if (gmonth == 4)  { m = 120; if (leapyear(gyear)) m++; }
	if (gmonth == 5)  { m = 151; if (leapyear(gyear)) m++; }
	if (gmonth == 6)  { m = 181; if (leapyear(gyear)) m++; }
	if (gmonth == 7)  { m = 212; if (leapyear(gyear)) m++; }
	if (gmonth == 8)  { m = 243; if (leapyear(gyear)) m++; }
	if (gmonth == 9)  { m = 273; if (leapyear(gyear)) m++; }
	if (gmonth == 10) { m = 304; if (leapyear(gyear)) m++; }
	if (gmonth == 11) { m = 334; if (leapyear(gyear)) m++; }
	if (gmonth == 12) { m = 365; if (leapyear(gyear)) m++; }
	months = age * 12;
	months += 12 - parseInt(mm);
	months += gmonth;
	totdays = (parseInt(age) * 365);
	totdays += age / 4;
	totdays = parseInt(totdays) + gdate + m + n;
	if (gmonth == 1) p = 31 + gdate;
	if (gmonth == 2)  { p = 59 + gdate;  if (leapyear(gyear)) m = m+1;}
	if (gmonth == 3)  { p = 90 + gdate;  if (leapyear(gyear)) p++; }
	if (gmonth == 4)  { p = 120 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 5)  { p = 151 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 6)  { p = 181 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 7)  { p = 212 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 8)  { p = 243 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 9)  { p = 273 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 10) { p = 304 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 11) { p = 334 + gdate; if (leapyear(gyear)) p++; }
	if (gmonth == 12) { p = 365 + gdate; if (leapyear(gyear)) p++; }
	weeks = (age * 365) + n + p;
	weeks = weeks / 7;
	etcdays = parseFloat(weeks) - parseInt(weeks);
	etcdays = Math.round(etcdays * 7);
	weeks = parseInt(weeks);
	etcdays += parseInt(age / 4);
	if (etcdays > 7)
	weeks += parseInt(etcdays / 7);
	
//	alert("你今年:"+age);
//	alert("或者 你出生至今 已經 " + weeks + " 個星期了\n");
//	alert("或者 你出生至今 已經 " + months + " 個月了\n");
//	alert("或者 你出生至今 已經 " + totdays + " 天了\n");
	
	var time = new Date();
	var ghour = time.getHours();
	var gmin = time.getMinutes();
	var gsec = time.getSeconds();
	var hour = ((age * 365) + n + p) * 24;
	hour += (parseInt(age / 4) * 24);	// 你出生至今 已經 " + hour + " 小時了
	var min = (hour * 60) + gmin;		// 你出生至今 已經 " + min + " 分了
	var sec = (min * 60) + gsec;		// 你出生至今 已經 " + sec + " 秒了
	mm = mm - 1;
	var r;
	if (mm == 0) r = 0;
	if (mm == 1) r = 31;
	if (mm == 2) { r = 59; if (leapyear(gyear)) m++; }
	if (mm == 3)  { r = 90;  if (leapyear(gyear)) r++; }
	if (mm == 4)  { r = 120; if (leapyear(gyear)) r++; }
	if (mm == 5)  { r = 151; if (leapyear(gyear)) r++; }
	if (mm == 6)  { r = 181; if (leapyear(gyear)) r++; }
	if (mm == 7)  { r = 212; if (leapyear(gyear)) r++; }
	if (mm == 8)  { r = 243; if (leapyear(gyear)) r++; }
	if (mm == 9)  { r = 273; if (leapyear(gyear)) r++; }
	if (mm == 10) { r = 304; if (leapyear(gyear)) r++; }
	if (mm == 11) { r = 334; if (leapyear(gyear)) r++; }
	mm = mm + 1;
	r = parseInt(r) + parseInt(dd);
	
	var bday;	// 天數
	var nhour = 24 - parseInt(ghour);	// 時
	var nmin = 60 - parseInt(gmin);		// 分
	var nsec = 60 - parseInt(gsec);		// 秒
	if ((mm >= (gmonth + 1)) && (dd > gdate)) {
		bday = r - m - gdate;
		}
	else {
		if ((leapyear(gyear)) && ((mm > 2) && (dd < 29))) {
			a = 366;
		} else {
			a = 365;
		}
		bday = a + (r - m - gdate);
	}

	
	while (bday > 366) bday -= 365;
	if (((bday == 366) && (leapyear(gyear)) || ((bday == 365) && (!leapyear(gyear))))) {
		// "\n\n今天是你的生日!";
	} else {
		//document.agecalc.timealive.value += "\n\n你離下一個生日還有:\n"
		//+ bday + " 天 " + nhour + " 小時 " + nmin + " 分 " + nsec + " 秒";
		//setTimeout("run()", 1000);
		
	} // end else
	
	var birthdayInfo = {
		
		'age' : age,			// 年齡
		'weeks' : weeks,		// 你出生至今 已經 " + weeks + " 個星期了
		'months' : months,		// 你出生至今 已經 " + months + " 個月了
		'totdays' : totdays,	// 你出生至今 已經 " + totdays + " 天了
		'hour' : hour,	// 你出生至今 已經 " + hour + " 小時了
		'min' : min,	// 你出生至今 已經 " + min + " 分了
		'sec' : sec,	// 你出生至今 已經 " + sec + " 秒了
		'bday' : bday,	// "你離下一個生日還有:"+ bday + " 天 " + nhour + " 小時 " + nmin + " 分 " + nsec + " 秒";
		'nhour' : nhour,
		'nmin' : nmin,
		'nsec' : nsec
	};
	return birthdayInfo;
	
} // end function calculateAge

//當欄位內容被改變就勾選對映的checkbox
function selectCheckbox(checkboxObj, name) {
	for ( var i = 0; i < checkboxObj.length; i++ ) {
		if (checkboxObj[i].value==name) {
			checkboxObj[i].checked=true;
		} // end if
	} // end for
} // end function